import csv,sys,math

NAs = ["na","nan","?",""]


def percentile(N, percent, key=lambda x:x):
    """
    Find the percentile of a list of values.
    
    @parameter N - is a list of values. Note N MUST BE already sorted.
    @parameter percent - a float value from 0.0 to 1.0.
    @parameter key - optional key function to compute value from each element of N.
    
    @return - the percentile of the values
    """

    if not N:
        return None

    k = (len(N)-1) * percent
    f = math.floor(k)
    c = math.ceil(k)

    if f == c:
        return key(N[int(k)])

    d0 = key(N[int(f)]) * (c-k)
    d1 = key(N[int(c)]) * (k-f)

    return d0+d1


def isNA(x):
    return x.lower() in NAs


assert sys.argv[1] != sys.argv[2]

matrixReader = csv.reader(open(sys.argv[1],'r'), delimiter='\t')
summaryWriter = csv.writer(open(sys.argv[2],'w'), delimiter='\t')

# Remove header line from matrix
matrixReader.next()

# Add header line to summary file
summaryWriter.writerow(['Percentile','10%','30%','50%','70%','90%'])

for line in matrixReader:

    header = line[0]

    # Skip nonnumerical features 
    if header[0] != 'N':
        continue

    data = sorted([float(x) for x in line[1:] if not isNA(x)])

    summaryWriter.writerow([header,percentile(data,0.1),percentile(data,0.3),percentile(data,0.5),percentile(data,0.7),percentile(data,0.9)])
    
